<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>解密导致并发问题的第二个幕后黑手：原子性问题 | 冰河技术</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script charset="utf-8" async="async" src="/js/jquery.min.js"></script>
    <script charset="utf-8" async="async" src="/js/global.js"></script>
    <script charset="utf-8" async="async" src="/js/fingerprint2.min.js"></script>
    <script charset="utf-8" async="async" src="https://v1.cnzz.com/z_stat.php?id=1281063564&amp;web_id=1281063564"></script>
    <script charset="utf-8" async="async" src="https://s9.cnzz.com/z_stat.php?id=1281064551&amp;web_id=1281064551"></script>
    <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?d091d2fd0231588b1d0f9231e24e3f5e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
            </script>
    <meta name="description" content="包含：编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧...">
    <meta property="article:modified_time" content="2022-05-23T11:30:51.000Z">
    <meta property="og:title" content="解密导致并发问题的第二个幕后黑手：原子性问题">
    <meta property="og:type" content="article">
    <meta property="og:url" content="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html">
    <meta name="twitter:title" content="解密导致并发问题的第二个幕后黑手：原子性问题">
    <meta name="twitter:url" content="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="robots" content="all">
    <meta name="author" content="冰河">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta name="keywords" content="冰河，冰河技术, 编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧">
    <meta name="apple-mobile-web-app-capable" content="yes">
    
    <link rel="preload" href="/assets/css/0.styles.ab888ebb.css" as="style"><link rel="preload" href="/assets/css/styles.css?v=1653305936337" as="style"><link rel="preload" href="/assets/js/cg-styles.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-app.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-4.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-3.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-21.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-5.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-6.js?v=1653305936337" as="script">
    <link rel="stylesheet" href="/assets/css/0.styles.ab888ebb.css"><link rel="stylesheet" href="/assets/css/styles.css?v=1653305936337">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">冰河技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>底层技术</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/bottom/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>源码分析</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="sidebar-link">一文搞懂线程与多线程</a></li><li><a href="/md/concurrent/source/2020-03-30-002-如何确保线程按照我们想要的顺序执行.html" class="sidebar-link">如何确保线程按照我们想要的顺序执行</a></li><li><a href="/md/concurrent/source/2020-03-30-003-深入解析Callable接口.html" class="sidebar-link">深入解析Callable接口</a></li><li><a href="/md/concurrent/source/2020-03-30-004-两种异步模型与深度解析Future接口.html" class="sidebar-link">两种异步模型与深度解析Future接口</a></li><li><a href="/md/concurrent/source/2020-03-30-005-SimpleDateFormat类到底为啥不是线程安全的？（附六种解决方案，建议收藏）.html" class="sidebar-link">SimpleDateFormat类到底为啥不是线程安全的？</a></li><li><a href="/md/concurrent/source/2020-03-30-006-不得不说的线程池与ThreadPoolExecutor类浅析.html" class="sidebar-link">不得不说的线程池与ThreadPoolExecutor类浅析</a></li><li><a href="/md/concurrent/source/2020-03-30-007-深度解析线程池中那些重要的顶层接口和抽象类.html" class="sidebar-link">深度解析线程池中那些重要的顶层接口和抽象类</a></li><li><a href="/md/concurrent/source/2020-03-30-008-从源码角度分析创建线程池究竟有哪些方式.html" class="sidebar-link">从源码角度分析创建线程池究竟有哪些方式</a></li><li><a href="/md/concurrent/source/2020-03-30-009-通过源码深度解析ThreadPoolExecutor类是如何保证线程池正确运行的.html" class="sidebar-link">通过源码深度解析ThreadPoolExecutor类是如何保证线程池正确运行的</a></li><li><a href="/md/concurrent/source/2020-03-30-010-通过ThreadPoolExecutor类的源码深度解析线程池执行任务的核心流程.html" class="sidebar-link">通过ThreadPoolExecutor类的源码深度解析线程池执行任务的核心流程</a></li><li><a href="/md/concurrent/source/2020-03-30-011-通过源码深度分析线程池中Worker线程的执行流程.html" class="sidebar-link">通过源码深度分析线程池中Worker线程的执行流程</a></li><li><a href="/md/concurrent/source/2020-03-30-012-从源码角度深度解析线程池是如何实现优雅退出的.html" class="sidebar-link">从源码角度深度解析线程池是如何实现优雅退出的</a></li><li><a href="/md/concurrent/source/2020-03-30-013-ScheduledThreadPoolExecutor与Timer的区别和简单示例.html" class="sidebar-link">ScheduledThreadPoolExecutor与Timer的区别和简单示例</a></li><li><a href="/md/concurrent/source/2020-03-30-014-深度解析ScheduledThreadPoolExecutor类的源代码.html" class="sidebar-link">深度解析ScheduledThreadPoolExecutor类的源代码</a></li><li><a href="/md/concurrent/source/2020-03-30-015-浅谈AQS中的CountDownLatch、Semaphore与CyclicBarrier.html" class="sidebar-link">浅谈AQS中的CountDownLatch、Semaphore与CyclicBarrier</a></li><li><a href="/md/concurrent/source/2020-03-30-016-浅谈AQS中的ReentrantLock、ReentrantReadWriteLock、StampedLock与Condition.html" class="sidebar-link">浅谈AQS中的ReentrantLock、ReentrantReadWriteLock、StampedLock与Condition</a></li><li><a href="/md/concurrent/source/2020-03-30-017-朋友去面试竟然栽在了Thread类的源码上.html" class="sidebar-link">朋友去面试竟然栽在了Thread类的源码上</a></li><li><a href="/md/concurrent/source/2020-03-30-018-如何使用Java7提供的ForkJoin框架实现高并发程序？.html" class="sidebar-link">如何使用Java7提供的ForkJoin框架实现高并发程序？</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>基础案例</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="sidebar-link">明明中断了线程，却为何不起作用呢？</a></li><li><a href="/md/concurrent/basics/2020-03-30-002-由InterruptedException异常引发的思考.html" class="sidebar-link">由InterruptedException异常引发的思考</a></li><li><a href="/md/concurrent/basics/2020-03-30-003-要想学好并发编程，关键是要理解这三个核心问题.html" class="sidebar-link">要想学好并发编程，关键是要理解这三个核心问题</a></li><li><a href="/md/concurrent/basics/2020-03-30-004-导致并发编程频繁出问题的“幕后黑手”.html" class="sidebar-link">要想学好并发编程，关键是要理解这三个核心问题</a></li><li><a href="/md/concurrent/basics/2020-03-30-005-解密诡异并发问题的第一个幕后黑手——可见性问题.html" class="sidebar-link">一文解密诡异并发问题的第一个幕后黑手：可见性问题</a></li><li><a href="/md/concurrent/basics/2020-03-30-006-解密导致并发问题的第二个幕后黑手——原子性问题.html" class="active sidebar-link">解密导致并发问题的第二个幕后黑手：原子性问题</a></li><li><a href="/md/concurrent/basics/2020-03-30-007-解密导致并发问题的第三个幕后黑手——有序性问题.html" class="sidebar-link">解密导致并发问题的第三个幕后黑手：有序性问题</a></li><li><a href="/md/concurrent/basics/2020-03-30-008-一文秒懂Happens-Before原则.html" class="sidebar-link">一文秒懂Happens-Before原则</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>实战案例</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/ActualCombat/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>面试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/interview/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>系统架构</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/concurrent/framework/default.html" class="sidebar-link">关于我</a></li></ul></section></li></ul> </aside> <div><main class="page"> <div class="theme-default-content content__default"><h1 id="【高并发】解密导致并发问题的第二个幕后黑手-原子性问题"><a href="#【高并发】解密导致并发问题的第二个幕后黑手-原子性问题" class="header-anchor">#</a> 【高并发】解密导致并发问题的第二个幕后黑手：原子性问题</h1> <p><strong>大家好，我是冰河~~</strong></p> <p>今天，我们继续大冰和小菜的故事。</p> <h2 id="写在前面"><a href="#写在前面" class="header-anchor">#</a> 写在前面</h2> <blockquote><p>大冰：小菜童鞋，昨天讲解的内容复习了吗？</p> <h1 id=""><a href="#" class="header-anchor">#</a></h1> <p>小菜：复习了大冰哥，昨天的内容干货满满啊，感觉自己收获很大。</p> <h1 id="-2"><a href="#-2" class="header-anchor">#</a></h1> <p>大冰：那你说说昨天都讲了哪些内容呢？</p> <h1 id="-3"><a href="#-3" class="header-anchor">#</a></h1> <p>小菜：昨天主要讲了线程的可见性和可见性问题。可见性是指一个线程对共享变量的修改，另一个线程能够立刻看到，如果不能立刻看到，就可能会产生可见性问题。在单核CPU上是不存在可见性问题的，可见性问题主要存在于运行在多核CPU上的并发程序。归根结底，可见性问题还是由CPU的缓存导致的，而缓存导致的可见性问题是导致诸多诡异的并发编程问题的“幕后黑手”之一。</p> <h1 id="-4"><a href="#-4" class="header-anchor">#</a></h1> <p>大冰：很好，小菜童鞋，复习的不错，今天，我们继续讲并发问题的第二个“幕后黑手”——线程切换带来的原子性问题，这个知识点也是非常重要的，一定要好好听。</p></blockquote> <h2 id="原子性"><a href="#原子性" class="header-anchor">#</a> 原子性</h2> <blockquote><p>原子性是指一个或者多个操作在CPU中执行的过程不被中断的特性。原子性操作一旦开始运行，就会一直到运行结束为止，中间不会有中断的情况发生。</p></blockquote> <p>我们也可以这样理解原子性，就是线程在执行一系列操作时，这些操作会被当做一个不可拆分的整体执行，这些操作要么全部执行，要么全部不执行，不会存在只执行一部分的情况，这就是原子性操作。</p> <p>关于原子性操作一个<strong>典型的场景就是转账</strong>，例如，小明和小刚的账户余额都是200元，此时小明给小刚转账100元，如果转账成功，则小明的账户余额为100元，小刚的账户余额为300元；如果转账失败，则小明和小刚的账户余额仍然为200元。不会存在小明账户为100元，小刚账户为200元，或者小明账户为200元，小刚账户为300元的情况。</p> <p>这里，小明给小刚转账100元的操作，就是一个原子性操作，它涉及小明账户余额减少100元，小刚账户余额增加100元的操作，这两个操作是一个不可分割的整体，要么全部执行，要么全部不执行。</p> <p>小明给小刚转账成功，则如下所示。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103809648.jpg" loading="lazy" class="lazy"></p> <p>小明给小刚转账失败，则如下所示。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103824196.jpg" loading="lazy" class="lazy"></p> <p>不会出现小明账户为100元，小刚账户为200元的情况。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103837345.jpg" loading="lazy" class="lazy"></p> <p>也不会出现小明账户为200元，小刚账户为300元的情况。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103853928.jpg" loading="lazy" class="lazy"></p> <h2 id="线程切换"><a href="#线程切换" class="header-anchor">#</a> 线程切换</h2> <blockquote><p>在并发编程中，往往设置的线程数目会大于CPU数目，而每个CPU在同一时刻只能被一个线程使用。而CPU资源的分配采用了时间片轮转策略，也就是给每个线程分配一个时间片，线程在这个时间片内占用CPU的资源来执行任务。当占用CPU资源的线程执行完任务后，会让出CPU的资源供其他线程运行，这就是任务切换，也叫做线程切换或者线程的上下文切换。</p></blockquote> <p>如果大家还是不太理解的话，我们可以用下面的图来模拟线程在CPU中的切换过程。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103915661.jpg" loading="lazy" class="lazy"></p> <p>在图中存在线程A和线程B两个线程，其中线程A和线程B中的每个小方块代表此时线程占有CPU资源并执行任务，这个小方块占有的时间，被称为时间片，在这个时间片中，占有CPU资源的线程会在CPU上执行，未占有CPU资源的线程则不会在CPU上执行。而每个虚线部分就代表了此时的线程不占用CPU资源。CPU会在线程A和线程B之间频繁切换。</p> <h2 id="原子性问题"><a href="#原子性问题" class="header-anchor">#</a> 原子性问题</h2> <p>理解了什么是原子性，再看什么是原子性问题就比较简单了。</p> <blockquote><p>原子性问题是指一个或者多个操作在CPU中执行的过程中出现了被中断的情况。</p></blockquote> <p>线程在执行某项操作时，此时如果CPU发生了线程切换，CPU转而去执行其他的任务，中断了当前线程执行的操作，这就会造成原子性问题。</p> <p>如果你还不能理解的话，我们来举一个例子：假设你在银行排队办理业务，小明在你前面，柜台的业务员为小明办理完业务，正好排到你时，此时银行下班了，柜台的业务员微笑着告诉你：实在不好意思，先生（女士），我们下班了，您明天再来吧！此时的你就好比是正好占有了CPU资源的线程，而柜台的业务员就是那颗发生了线程切换的CPU，她将线程切换到了下班这个线程，执行下班的操作去了。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/2020032510393220.jpg" loading="lazy" class="lazy"></p> <h2 id="java中的原子性问题"><a href="#java中的原子性问题" class="header-anchor">#</a> Java中的原子性问题</h2> <p>在Java中，并发程序是基于多线程技术来编写的，这也会涉及到CPU的对于线程的切换问题，<strong>正是CPU中对任务的切换机制，导致了并发编程会出现原子性的诡异问题，而原子性问题，也成为了导致并发问题的第二个“幕后黑手”。</strong></p> <p>在并发编程中，往往Java语言中一条简单的语句，会对应着CPU中的多条指令，假设我们编写的ThreadTest类的代码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">io<span class="token punctuation">.</span>mykit<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span>lab01</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 测试原子性
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThreadTest</span> <span class="token punctuation">{</span>

    <span class="token keyword">private</span> <span class="token class-name">Long</span> count<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Long</span> <span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> count<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">incrementCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        count<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>接下来，我们打开ThreadTest类的class文件所在的目录，在cmd命令行输入如下命令。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>javap <span class="token operator">-</span>c <span class="token class-name">ThreadTest</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>得出如下的结果信息，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>d<span class="token operator">:</span><span class="token operator">&gt;</span>javap <span class="token operator">-</span>c <span class="token class-name">ThreadTest</span>
<span class="token class-name">Compiled</span> from <span class="token string">&quot;ThreadTest.java&quot;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name"><span class="token namespace">io<span class="token punctuation">.</span>mykit<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span>lab01<span class="token punctuation">.</span></span>ThreadTest</span> <span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token class-name"><span class="token namespace">io<span class="token punctuation">.</span>mykit<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span>lab01<span class="token punctuation">.</span></span>ThreadTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Code</span><span class="token operator">:</span>
       <span class="token number">0</span><span class="token operator">:</span> aload_0
       <span class="token number">1</span><span class="token operator">:</span> invokespecial #<span class="token number">1</span>                  <span class="token comment">// Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V</span>
       <span class="token number">4</span><span class="token operator">:</span> <span class="token keyword">return</span>

  <span class="token keyword">public</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>Long</span> <span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Code</span><span class="token operator">:</span>
       <span class="token number">0</span><span class="token operator">:</span> aload_0
       <span class="token number">1</span><span class="token operator">:</span> getfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
       <span class="token number">4</span><span class="token operator">:</span> areturn

  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">incrementCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Code</span><span class="token operator">:</span>
       <span class="token number">0</span><span class="token operator">:</span> aload_0
       <span class="token number">1</span><span class="token operator">:</span> getfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
       <span class="token number">4</span><span class="token operator">:</span> astore_1
       <span class="token number">5</span><span class="token operator">:</span> aload_0
       <span class="token number">6</span><span class="token operator">:</span> aload_0
       <span class="token number">7</span><span class="token operator">:</span> getfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
      <span class="token number">10</span><span class="token operator">:</span> invokevirtual #<span class="token number">3</span>                  <span class="token comment">// Method java/lang/Long.longValue:()J</span>
      <span class="token number">13</span><span class="token operator">:</span> lconst_1
      <span class="token number">14</span><span class="token operator">:</span> ladd
      <span class="token number">15</span><span class="token operator">:</span> invokestatic  #<span class="token number">4</span>                  <span class="token comment">// Method java/lang/Long.valueOf:(J)Ljava/lang/Long;</span>
      <span class="token number">18</span><span class="token operator">:</span> dup_x1
      <span class="token number">19</span><span class="token operator">:</span> putfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
      <span class="token number">22</span><span class="token operator">:</span> astore_2
      <span class="token number">23</span><span class="token operator">:</span> aload_1
      <span class="token number">24</span><span class="token operator">:</span> pop
      <span class="token number">25</span><span class="token operator">:</span> <span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p>这里，我们主要关注下incrementCount()方法对应的CPU指令，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">incrementCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Code</span><span class="token operator">:</span>
       <span class="token number">0</span><span class="token operator">:</span> aload_0
       <span class="token number">1</span><span class="token operator">:</span> getfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
       <span class="token number">4</span><span class="token operator">:</span> astore_1
       <span class="token number">5</span><span class="token operator">:</span> aload_0
       <span class="token number">6</span><span class="token operator">:</span> aload_0
       <span class="token number">7</span><span class="token operator">:</span> getfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
      <span class="token number">10</span><span class="token operator">:</span> invokevirtual #<span class="token number">3</span>                  <span class="token comment">// Method java/lang/Long.longValue:()J</span>
      <span class="token number">13</span><span class="token operator">:</span> lconst_1
      <span class="token number">14</span><span class="token operator">:</span> ladd
      <span class="token number">15</span><span class="token operator">:</span> invokestatic  #<span class="token number">4</span>                  <span class="token comment">// Method java/lang/Long.valueOf:(J)Ljava/lang/Long;</span>
      <span class="token number">18</span><span class="token operator">:</span> dup_x1
      <span class="token number">19</span><span class="token operator">:</span> putfield      #<span class="token number">2</span>                  <span class="token comment">// Field count:Ljava/lang/Long;</span>
      <span class="token number">22</span><span class="token operator">:</span> astore_2
      <span class="token number">23</span><span class="token operator">:</span> aload_1
      <span class="token number">24</span><span class="token operator">:</span> pop
      <span class="token number">25</span><span class="token operator">:</span> <span class="token keyword">return</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>可以看到，Java语言中短短的几行incrementCount()方法竟然对应着那么多的CPU指令。这些CPU指令我们大致可以分成三步。</p> <ul><li>指令1：把变量count从内存加载的CPU寄存器。</li> <li>指令2：在寄存器中执行count++操作。</li> <li>指令3：将结果写入缓存（可能是CPU缓存，也可能是内存）。</li></ul> <p>在操作系统执行线程切换时，可能发生在任何一条CPU指令完成后，而不是程序中的某条语句完成后。如果线程A执行完指令1后，操作系统发生了线程切换，当两个线程都执行count++操作后，得到的结果是1而不是2。这里，我们可以使用下图来表示这个过程。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200325103957586.jpg" loading="lazy" class="lazy"></p> <p>由上图，我们可以看出：线程A将count=0加载到CPU的寄存器后，发生了线程切换。此时内存中的count值仍然为0，线程B将count=0加载到寄存器，执行count++操作，并将count=1写到内存。此时，CPU切换到线程A，执行线程A中的count++操作后，线程A中的count值为1，线程A将count=1写入内存，此时内存中的count值最终为1。</p> <p>所以，<strong>如果在CPU中存在正在执行的线程，恰好此时CPU发生了线程切换，则可能会导致原子性问题，这也是导致并发编程频繁出问题的根源之一</strong>。我们只有充分理解并掌握线程的原子性以及引起原子性问题的根源，并在日常工作中时刻注意编写的并发程序是否存在原子性问题，才能更好的编写出并发程序。</p> <h2 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h2> <p><strong>缓存带来的可见性问题、线程切换带来的原子性问题和编译优化带来的有序性问题，是导致并发编程频繁出现诡异问题的三个源头</strong>，我们已经介绍了缓存带来的可见性问题和线程切换带来的原子性问题。下一篇中，我们继续深耕高并发中的有序性问题。</p> <h2 id="写在最后"><a href="#写在最后" class="header-anchor">#</a> 写在最后</h2> <blockquote><p>大冰：好了，今天就是我们讲的主要内容了，今天的内容同样最重要，回去后要好好复习。</p> <h1 id="-5"><a href="#-5" class="header-anchor">#</a></h1> <p>小菜：好的，大冰哥，一定好好复习。</p></blockquote> <h2 id="文末福利"><a href="#文末福利" class="header-anchor">#</a> 文末福利</h2> <p>最后，附上并发编程需要掌握的核心技能知识图，祝大家在学习并发编程时，少走弯路。</p> <p><img alt="" data-src="https://img-blog.csdnimg.cn/2020032510405471.jpg" loading="lazy" class="lazy"></p> <h2 id="写在最后-2"><a href="#写在最后-2" class="header-anchor">#</a> 写在最后</h2> <blockquote><p>如果你觉得冰河写的还不错，请微信搜索并关注「 <strong>冰河技术</strong> 」微信公众号，跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术，「 <strong>冰河技术</strong> 」微信公众号更新了大量技术专题，每一篇技术文章干货满满！不少读者已经通过阅读「 <strong>冰河技术</strong> 」微信公众号文章，吊打面试官，成功跳槽到大厂；也有不少读者实现了技术上的飞跃，成为公司的技术骨干！如果你也想像他们一样提升自己的能力，实现技术能力的飞跃，进大厂，升职加薪，那就关注「 <strong>冰河技术</strong> 」微信公众号吧，每天更新超硬核技术干货，让你对如何提升技术能力不再迷茫！</p></blockquote> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200906013715889.png" loading="lazy" class="lazy"></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/binghe001/BingheGuide/edit/master/docs/md/concurrent/basics/2020-03-30-006-解密导致并发问题的第二个幕后黑手——原子性问题.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/5/23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/md/concurrent/basics/2020-03-30-005-解密诡异并发问题的第一个幕后黑手——可见性问题.html" class="prev">
          一文解密诡异并发问题的第一个幕后黑手：可见性问题
        </a></span> <span class="next"><a href="/md/concurrent/basics/2020-03-30-007-解密导致并发问题的第三个幕后黑手——有序性问题.html">
          解密导致并发问题的第三个幕后黑手：有序性问题
        </a>
        →
      </span></p></div> </main></div> <aside class="page-sidebar"> <div class="page-side-toolbar"><div class="option-box-toc-fixed"><div class="toc-container-sidebar"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:650px"><div style="font-weight:bold;text-align:center;">解密导致并发问题的第二个幕后黑手：原子性问题</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在前面" class="toc-sidebar-link">写在前面</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#原子性" class="toc-sidebar-link">原子性</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#线程切换" class="toc-sidebar-link">线程切换</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#原子性问题" class="toc-sidebar-link">原子性问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#java中的原子性问题" class="toc-sidebar-link">Java中的原子性问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#总结" class="toc-sidebar-link">总结</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#文末福利" class="toc-sidebar-link">文末福利</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在最后-2" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box-toc-over"><img src="/images/system/toc.png" class="nozoom"> <span class="show-txt">目录</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:550px"><div style="font-weight:bold;text-align:center;">解密导致并发问题的第二个幕后黑手：原子性问题</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在前面" class="toc-sidebar-link">写在前面</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#原子性" class="toc-sidebar-link">原子性</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#线程切换" class="toc-sidebar-link">线程切换</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#原子性问题" class="toc-sidebar-link">原子性问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#java中的原子性问题" class="toc-sidebar-link">Java中的原子性问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#总结" class="toc-sidebar-link">总结</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#文末福利" class="toc-sidebar-link">文末福利</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html#写在最后-2" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box"><img src="/images/system/wechat.png" class="nozoom"> <span class="show-txt">手机看</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.9rem">微信扫一扫</span> <img height="180px" src="https://api.qrserver.com/v1/create-qr-code/?data=https://binghe001.github.io/md/concurrent/basics/2020-03-30-006-%E8%A7%A3%E5%AF%86%E5%AF%BC%E8%87%B4%E5%B9%B6%E5%8F%91%E9%97%AE%E9%A2%98%E7%9A%84%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%B9%95%E5%90%8E%E9%BB%91%E6%89%8B%E2%80%94%E2%80%94%E5%8E%9F%E5%AD%90%E6%80%A7%E9%97%AE%E9%A2%98.html" style="margin:10px;">
                可以<b>手机看</b>或分享至<b>朋友圈</b></div></div></div></div> <div class="option-box"><img src="/images/system/toggle.png" width="30px" class="nozoom"> <span class="show-txt">左栏</span></div> <div class="option-box"><img src="/images/system/xingqiu.png" width="25px" class="nozoom"> <span class="show-txt">星球</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">实战项目<span style="font-size:8px;color:red;">「SpringCloud Alibaba实战项目」</span>、专属电子书、问题解答、简历指导、技术分享、晋升指导、视频课程</span> <img height="180px" src="/images/personal/xingqiu.png" style="margin:10px;"> <b>知识星球</b>：冰河技术
            </div></div></div></div> <div class="option-box"><img src="/images/system/wexin4.png" width="25px" class="nozoom"> <span class="show-txt">读者群</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">添加冰河微信<span style="color:red;">(hacker_binghe)</span>进冰河技术学习交流圈「无任何套路」</span> <img src="/images/personal/hacker_binghe.jpg" height="180px" style="margin:10px;">
                PS：添加时请备注<b>读者加群</b>，谢谢！
              </div></div></div></div> <div class="option-box"><img src="/images/system/download-2.png" width="25px" class="nozoom"> <span class="show-txt">下资料</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">扫描公众号，回复<span style="color:red;">“1024”</span>下载<span style="color:red;">100GB+</span>学习技术资料、PDF书籍、实战项目、简历模板等「无任何套路」</span> <img src="/images/personal/qrcode.png" height="180px" style="margin:10px;"> <b>公众号:</b> 冰河技术
              </div></div></div></div> <div class="option-box"><img src="/images/system/heart-1.png" width="25px" class="nozoom"> <span class="show-txt">赞赏我</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">鼓励/支持/赞赏我</span> <img height="180px" src="/images/personal/encourage-head.png" style="margin:5px;"> <br>1. 不靠它生存但仍希望得到你的鼓励；
                <br>2. 时刻警醒自己保持技术人的初心；
              </div></div></div></div> <div title="一文解密诡异并发问题的第一个幕后黑手：可见性问题" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/concurrent/basics/2020-03-30-005-解密诡异并发问题的第一个幕后黑手——可见性问题.html"><img src="/images/system/pre2.png" width="30px" class="nozoom"> <span class="show-txt">上一篇</span></a></div> <div title="解密导致并发问题的第三个幕后黑手：有序性问题" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/concurrent/basics/2020-03-30-007-解密导致并发问题的第三个幕后黑手——有序性问题.html"><img src="/images/system/next2.png" width="30px" class="nozoom"> <span class="show-txt">下一篇</span></a></div></div>  <!----> </aside></div><div class="global-ui"><div class="read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="read-more-btn" target="_self" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">阅读全文</a> <div id="btw-modal-wrap" style="display: none;"><div id="btw-mask" style="position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.7; z-index: 999; background: rgb(0, 0, 0);"></div> <div id="btw-modal" style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; text-align: center; font-size: 13px; background: rgb(255, 255, 255); border-radius: 10px; z-index: 9999; font-family: PingFangSC-Regular, sans-serif;"><span id="btw-modal-close-btn" style="position: absolute; top: 5px; right: 15px; line-height: 34px; font-size: 34px; cursor: pointer; opacity: 0.2; z-index: 9999; color: rgb(0, 0, 0); background: none; border: none; outline: none;">×</span> <p id="btw-modal-header" style="margin-top: 40px; line-height: 1.8; font-size: 13px;">
                扫码或搜索：<span style="color: #E9405A; font-weight: bold;">冰河技术</span> <br>发送：<span id="fustack-token" class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">290992</span> <br>即可<span style="color: #e9415a; font-weight: bold;">立即永久</span>解锁本站全部文章</p> <img src="/images/personal/qrcode.png" style="width: 180px; margin-top: 10px; margin-bottom: 30px; border: 8px solid rgb(230, 230, 230);"></div></div></div><div class="pay-read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="pay-read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="pay-read-more-btn" target="_blank" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">付费阅读</a></div></div></div>
    <script src="/assets/js/cg-styles.js?v=1653305936337" defer></script><script src="/assets/js/cg-4.js?v=1653305936337" defer></script><script src="/assets/js/cg-3.js?v=1653305936337" defer></script><script src="/assets/js/cg-21.js?v=1653305936337" defer></script><script src="/assets/js/cg-5.js?v=1653305936337" defer></script><script src="/assets/js/cg-6.js?v=1653305936337" defer></script><script src="/assets/js/cg-app.js?v=1653305936337" defer></script>
  </body>
</html>
